﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for admins data manipulation
    /// </summary>
    public class AdminsRepository : BaseRepository<Admin>, IAdminsRepository
    {
        private IUsersRepository _usersRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="AdminsRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public AdminsRepository(IDataServiceContext<Admin> dataServiceContext, IUsersRepository usersRepository)
            :base(dataServiceContext)
        {
            _usersRepository = usersRepository;
        }

        public override void Add(Admin entity)
        {
            var user = _usersRepository.Get(entity.User);

            if (user == null)
                throw new ArgumentException("User nor valid");
            base.Add(entity);
        }

        /// <summary>
        /// Determines whether the specified user is admin.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>
        /// 	<c>true</c> if the specified users is admin; otherwise, <c>false</c>.
        /// </returns>
        public bool IsAdmin(string userID)
        {
            return Entities.Any(a => a.User == userID);
        }
    }
}
